//
//  VRFollowViewController.swift
//  VoiceRoom
//
//  Created by Rae on 2024/3/15.
//

import UIKit

class VRFollowViewController: VRBaseViewController {

    private let backButton          = c_make_ex(UIButton.self)
        .image("return").end()
    var titleLabel          = c_make_ex(UILabel.self)
        .color(.white).font(20.font_bold)
        .alignment(.center).end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.backgroundColor = .clear
        tableView.separatorStyle = .none
        tableView.delegate = self
        tableView.dataSource = self
        tableView.estimatedRowHeight = 80
        tableView.register(VRFriendsCell.self)
        return tableView
    }()
    private let emptyView = VRContentEmptyView()
    var datasource:[InfoUtil.initInfo?] = []
    //1.following 2.follower 3.block
    var type: Int = 1 {
        didSet{
            if type == 1 {
                titleLabel.text = "Following"
            }else if type == 2 {
                titleLabel.text = "Follower"
            }else if type == 3 {
                titleLabel.text = "Blocklist"
            }
            loadData()
        }
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            titleLabel
            tableView
            emptyView
        }
        
        backButton.top(.statusHeight + 2).leading(5).size(40)
        titleLabel.centerHorizontally()
        titleLabel.CenterY == backButton.CenterY
        tableView.leading(0).trailing(0).top(.navi_height).bottom(.safe_bottom)
        emptyView.top(.navi_height + 100).leading(0).trailing(0).height(180)
        emptyView.isHidden = true
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }
    
    private func loadData(){
        
        if type == 1 {
            RT.FriendListAction { list in
                self.datasource = list
            }
        }else if type == 2 {
            RT.fansListAction { list in
                self.datasource = list
            }
        }else if type == 3 {
            RT.blockListWith { list in
                self.datasource = list
            }
        }
        self.tableView.reloadData()
    }
}

extension VRFollowViewController: UITableViewDelegate,UITableViewDataSource{
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if datasource.count == 0 {
            emptyView.isHidden = false
        }else{
            emptyView.isHidden = true
        }
        return datasource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.cell(VRFriendsCell.self, indexPath: indexPath)
        if type == 1 {
            cell.followingModel = datasource[indexPath.row]
        }else if type == 2 {
            cell.followerModel = datasource[indexPath.row]
        }else if type == 3 {
            cell.blockModel = datasource[indexPath.row]
        }
        cell.followBlock = { userId in
            RT.AddDeleteFriend(idstring: userId, type: "1") { s in
                self.loadData()
            }
        }
        cell.unfollowBlock = { userId in
            RT.AddDeleteFriend(idstring: userId, type: "0") { s in
                self.loadData()
            }
        }
        cell.removeBlock = { userId in
            RT.blockWithUserid(uid: userId, type: "0") { code in
                if code == "1" {
                    self.loadData()
                }
            }
        }
        return cell
    }
}

class VRFriendsCell: UITableViewCell {

    private let iconImageView          = c_make_ex(UIImageView.self)
        .corner(25).mode(.scaleAspectFill).end()
    private let nameLabel          = c_make_ex(UILabel.self)
        .color(.white).font(16.font_semiBold).end()
    private let subLabel          = c_make_ex(UILabel.self)
        .color(.white.alpha(0.4)).font(14.font_medium).end()
    private let followBtn          = c_make_ex(UIButton.self)
        .corner(16).font(14.font_semiBold).end()
    //1.following 2.follower 3.block
    private var type: Int = 1
    
    var unfollowBlock: ((_ userId: String) -> Void)?
    var followBlock: ((_ userId: String) -> Void)?
    var removeBlock: ((_ userId: String) -> Void)?

    
    var followingModel: InfoUtil.initInfo? {
        didSet{
            guard let md = followingModel else { return }
            type = 1
            iconImageView.loadImage(url: md.headImage)
            nameLabel.text = md.name
            subLabel.text = md.info1
            followBtn.setTitleColor(.white.alpha(0.6), for: .normal)
            followBtn.backgroundColor = .clear
            followBtn.setTitle("Unfollow", for: .normal)
            followBtn.layer.borderWidth = 2
            followBtn.layer.borderColor = UIColor.white.alpha(0.2).cgColor
            followBtn.layer.masksToBounds = true
        }
    }
    
    var followerModel: InfoUtil.initInfo? {
        didSet{
            guard let md = followerModel else { return }
            type = 2
            iconImageView.loadImage(url: md.headImage)
            nameLabel.text = md.name
            subLabel.text = md.info1
            followBtn.setTitleColor(.black, for: .normal)
            followBtn.backgroundColor = "#FFFC00".toColor.alpha(0.9)
            followBtn.setTitle("Follow", for: .normal)
            followBtn.layer.borderColor = UIColor.clear.cgColor
            followBtn.layer.masksToBounds = true
        }
    }
    
    var blockModel: InfoUtil.initInfo? {
        didSet{
            guard let md = blockModel else { return }
            type = 3
            iconImageView.loadImage(url: md.headImage)
            nameLabel.text = md.name
            subLabel.text = md.info1
            followBtn.setTitleColor(.black, for: .normal)
            followBtn.backgroundColor = "#FFFC00".toColor.alpha(0.9)
            followBtn.setTitle("Remove", for: .normal)
            followBtn.layer.borderColor = UIColor.clear.cgColor
            followBtn.layer.masksToBounds = true
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews {
            iconImageView
            nameLabel
            subLabel
            followBtn
        }
        
        iconImageView.leading(15).size(50).centerVertically()
        nameLabel.leading(75).top(20)
        subLabel.leading(75).top(44.5)
        followBtn.width(90).trailing(15).height(32).centerVertically()
        
        followBtn.addTarget(self, action: #selector(clickButton), for: .touchUpInside)
    }
    
    @objc private func clickButton(){
        
        if type == 1 {
            unfollowBlock?(followingModel?.id ?? "")
        }else if type == 2 {
            followBlock?(followerModel?.id ?? "")
        }else if type == 3 {
            removeBlock?(blockModel?.id ?? "")
        }
    }

    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
